package com.spgf.model.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.archtecture.model.exceptions.NegocioException;
import com.spgf.model.dao.util.PadraoDAOAb;
import com.spgf.model.entities.CategoriaModel;
import com.spgf.model.entities.IdentificacaoCategoriaModel;
import com.spgf.model.entities.utils.ModelAb;

public class IdentificacaoCategoriaDAO extends PadraoDAOAb {

	public final static String COLUNA_CODIGO = "CD_IDENTIFICACAO_CATEGORIA";
	public final static String COLUNA_DS_CATEGORIA = "DS_CATEGORIA";
	public final static String COLUNA_CATEGORIA = "CD_CATEGORIA";

	public final static String NOME_TABELA = "TBIDENTIFICACAO_CATEGORIA";

	public IdentificacaoCategoriaDAO(Context context) {
		super(context);
	}

	/**
	 * @return
	 * @throws NegocioException
	 */
	protected String getOrderBy() throws NegocioException {
		return " " + getNomeColunaCodigo() + " DESC ";
	}

	@Override
	public <Model extends ModelAb> String getClausuraWherePadrao(Model pModel) throws NegocioException {
		return "";
	}

	@Override
	public String[] getColuns() {
		return new String[] { COLUNA_CODIGO, COLUNA_DS_CATEGORIA, COLUNA_CATEGORIA };
	}

	@Override
	@SuppressWarnings("unchecked")
	protected <Model extends ModelAb> Model getModel(Cursor cursor) throws NegocioException {

		final IdentificacaoCategoriaModel newModel = new IdentificacaoCategoriaModel();

		if (cursor.getColumnIndex(COLUNA_CODIGO) != -1) {
			newModel.setCodigo(cursor.getInt(cursor.getColumnIndex(COLUNA_CODIGO)));
		}

		if (cursor.getColumnIndex(COLUNA_DS_CATEGORIA) != -1) {
			newModel.setDescricaoCategoria(cursor.getString(cursor.getColumnIndex(COLUNA_DS_CATEGORIA)));
		}

		if (cursor.getColumnIndex(COLUNA_CATEGORIA) != -1) {
			newModel.setCategoria(new CategoriaModel(cursor.getInt(cursor.getColumnIndex(COLUNA_CATEGORIA))));
		}

		return (Model) newModel;
	}

	@Override
	protected String getNomeColunaCodigo() {
		return COLUNA_CODIGO;
	}

	@Override
	protected String getNomeTabela() {
		return NOME_TABELA;
	}

	@Override
	protected <Model extends ModelAb> ContentValues getValues(Model pModel) throws NegocioException {

		IdentificacaoCategoriaModel lModel = (IdentificacaoCategoriaModel) pModel;

		final ContentValues values = new ContentValues();
		values.put(COLUNA_DS_CATEGORIA, lModel.getDescricaoCategoria());
		values.put(COLUNA_CATEGORIA, lModel.getCategoria().getCodigo());
		return values;
	}

	@Override
	public <Model extends ModelAb> Model getDependencias(Model pModel) throws NegocioException {
		IdentificacaoCategoriaModel lIdentCateg = (IdentificacaoCategoriaModel) pModel;
		if (lIdentCateg.getCategoria() != null) {
			lIdentCateg.setCategoria(new CategoriaDAO(getContext()).obterPorCodigo(new CategoriaModel(lIdentCateg.getCategoria().getCodigo())));
		}
		return pModel;
	}

}
